package com.zjsru.oneDay;

/**
 * 有效的井字游戏
 *
 * */
public class jinziyouxi {
    public boolean validTicTacToe(String[] board) {
        int timeX = 0;
        int timeO = 0;
        char[][] chars = new char[3][3];
        for (int i = 0; i < 3; i++) {
            char[] row = board[i].toCharArray();
            for (int j = 0; j < 3; j++) {
                chars[i][j] = row[j];
                if (chars[i][j] == 'X') timeX++;
                else if (chars[i][j] == 'O') timeO++;
            }
        }
        int[] sameRow = new int[3];
        int[] sameCole = new int[3];
        int sameDiagonalLeft = 0;//左斜
        int sameDiagonRight = 0;//右斜
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (chars[i][j] == ' ') continue;
                if (i > 0 && chars[i][j] == chars[i - 1][j]) sameCole[j]++;
                if (j > 0 && chars[i][j] == chars[i][j - 1]) sameRow[i]++;
                if (i > 0 && i == j && chars[i][j] == chars[i - 1][j - 1]) sameDiagonalLeft++;
                if (j < 2 && i == 2 - j && chars[i][j] == chars[i - 1][j + 1]) sameDiagonRight++;
//                游戏结束条件
                if (sameCole[j] == 2 || sameRow[i] == 2) {
                    if (chars[i][j] == 'O' && timeX == timeO) return true;
                    if (chars[i][j] == 'X' && timeX == timeO + 1) return true;
                    return false;
                }
                if (sameDiagonalLeft == 2 || sameDiagonRight == 2) {
                    if (chars[1][1] == 'O' && timeX == timeO) return true;
                    else if (chars[1][1] == 'X' && timeX == timeO + 1) return true;
                    return false;
                }
            }
        }
        if (timeX - 1 > timeO || timeO > timeX) return false;
        return true;
    }
    public static void main(String[] args) {
        jinziyouxi jinziyouxi = new jinziyouxi();
        System.out.println(jinziyouxi.validTicTacToe(new String[]{"XOX", "   ", "   "}));
    }
}
